// Created by carolsail

import { ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import BaseLayer from "./Baselayer";
import { random } from "../Utils";
import ResourceManager from "../manager/ResourceManager";
import DataManager from "../manager/DataManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ReadyLayer extends BaseLayer {

    // skeleton: sp.Skeleton = null
    loadingLabel: cc.Label = null

    onLoad() {
        const spineNode = this.node.getChildByName('spine_node')
        // this.skeleton = spineNode.getComponent(sp.Skeleton)
        const loadNode = this.node.getChildByName('loading')
        this.loadingLabel = loadNode.getComponent(cc.Label)
    }

     onEnable() {
        let timer = 0, count = 0, seconds = random(2, 4)
        // this.skeleton.setAnimation(0, 'transitions_bottom_close', false)
        this.loadingLabel.string = '匹配中'
        this.loadingLabel.unscheduleAllCallbacks()
         this.loadingLabel.schedule( async () => {
            timer++
            count++
            count = count % 4
            let dot = ''
            if (count == 1) dot = '.'
            if (count == 2) dot = '..'
            if (count == 3) dot = '...'
            this.loadingLabel.string = `匹配中${dot}`
            if (timer >= seconds) {
                this.loadingLabel.unscheduleAllCallbacks()
                this.loadingLabel.string = '匹配成功'
                await StaticInstance.fadeManager.fadeIn()
                const users = ResourceManager.instance.getJson('users')
                DataManager.instance.levelAIClear = 0
                DataManager.instance.currentAI = users[random(0, users.length - 1)]
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.READY, false)
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MAIN_PK)
                StaticInstance.gameManager.onGameStart()
             // 设置骨架动画为“transitions_bottom_open”，不循环播放
            //     const entry = this.skeleton.setAnimation(0, 'transitions_bottom_open', false);
            //  // 设置动画轨道完成监听器
            //     this.skeleton.setTrackCompleteListener(entry, async () => {
            //         await StaticInstance.fadeManager.fadeIn()
            //         const users = ResourceManager.instance.getJson('users')
            //         DataManager.instance.levelAIClear = 0
            //         DataManager.instance.currentAI = users[random(0, users.length - 1)]
            //         StaticInstance.uiManager.toggle(ENUM_UI_TYPE.READY, false)
            //         StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MAIN_PK)
            //         StaticInstance.gameManager.onGameStart()
            //     })
            }
        }, 1)
    }

    onDisable() {
        this.loadingLabel.unscheduleAllCallbacks()
    }
}